package com.example.tuter.util;

import static com.example.tuter.util.Constant.CUR_WEEKS;
import static com.example.tuter.util.Constant.schoolCookie;
import static com.example.tuter.util.Constant.token;
import static com.example.tuter.util.Constant.userInfo;
import static com.example.tuter.util.SharedPreferencesUtil.SPConstant.USER_TOKEN;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.example.tuter.entity.SchoolCookie;
import com.example.tuter.entity.UserInfo;
import com.example.tuter.fragment.entity.CoursesKB;
import com.example.tuter.fragment.entity.TimeSemester;

public class SPUtils {
    public static UserInfo getUserInfo(Context context) {
        if (userInfo != null) return userInfo;
        String jsonString = (String) SharedPreferencesUtil.get(context, SharedPreferencesUtil.SPConstant.USERINFO, "");
        UserInfo userInfo = null;
        try {
            userInfo = JSON.parseObject(jsonString, UserInfo.class);
        } catch (Exception e) {
            putUserInfo(context, new UserInfo());
        }
        return userInfo;

    }

    public static void clear(Context context) {
        SharedPreferencesUtil.remove(context, SharedPreferencesUtil.SPConstant.SCHOOL_TOKEN);
        SharedPreferencesUtil.remove(context, SharedPreferencesUtil.SPConstant.USER_TOKEN);
        SharedPreferencesUtil.remove(context, SharedPreferencesUtil.SPConstant.USERINFO);

        //清空个人信息
        token = null;
        schoolCookie = null;
        userInfo = null;
    }

    public static void putUserInfo(Context context, UserInfo userInfo) {
        Constant.userInfo = userInfo;
        SharedPreferencesUtil.put(context, SharedPreferencesUtil.SPConstant.USERINFO, JSON.toJSONString(userInfo));
    }

    public static String getToken(Context context) {
        if (token != null) return token;
        token = (String) SharedPreferencesUtil.get(context, SharedPreferencesUtil.SPConstant.USER_TOKEN, "");
        return token;
    }

    public static void putToken(Context context, String token) {
        Constant.token = token;
        SharedPreferencesUtil.put(context, USER_TOKEN, token);
    }


    public static void putSchoolCookie(Context context, SchoolCookie schoolCookie) {
        Constant.schoolCookie = schoolCookie;
        SharedPreferencesUtil.put(context, SharedPreferencesUtil.SPConstant.SCHOOL_TOKEN, JSON.toJSONString(schoolCookie));
    }

    public static SchoolCookie getSchoolCookie(Context context) {
        if (schoolCookie != null) return schoolCookie;
        String jsonString = (String) SharedPreferencesUtil.get(context, SharedPreferencesUtil.SPConstant.SCHOOL_TOKEN, "");
        SchoolCookie schoolCookie = null;
        try {
            schoolCookie = JSON.parseObject(jsonString, SchoolCookie.class);
        } catch (Exception e) {
            putSchoolCookie(context, new SchoolCookie());
        }
        return schoolCookie;

    }


    public static void putCurSemester(Context context, String semester) {
        Constant.CUR_SEMESTER = semester;
        SharedPreferencesUtil.put(context, SharedPreferencesUtil.SPConstant.CUR_SEMESTER, semester);
    }

    public static void putCurWeek(Context context, Integer week) {
        Constant.CUR_WEEKS = week;
        SharedPreferencesUtil.put(context, SharedPreferencesUtil.SPConstant.CUR_WEEK, week);
    }

    public static String getCurSemester(Context context) {
        if (Constant.CUR_SEMESTER != null) return Constant.CUR_SEMESTER;

        return (String) SharedPreferencesUtil.get(context, SharedPreferencesUtil.SPConstant.CUR_SEMESTER, "");
    }

    public static Integer getCurWeek(Context context) {
        if (CUR_WEEKS != null) return CUR_WEEKS;
        return (Integer) SharedPreferencesUtil.get(context, SharedPreferencesUtil.SPConstant.CUR_WEEK, -1);
    }


    public static void putCourse(Context context, String semester, CoursesKB coursesKB) {
        SharedPreferencesUtil.put(context, Constant.userInfo.getStudentId() + semester, JSON.toJSONString(coursesKB));
    }

    public static CoursesKB getCourseBySemester(Context context, String semester) {
        if (userInfo == null || userInfo.getStudentId() == null) return null;
        CoursesKB coursesKB = null;
        try {
            coursesKB = JSON.parseObject((String) SharedPreferencesUtil.get(context, Constant.userInfo.getStudentId() + semester, ""), CoursesKB.class);
        } catch (Exception e) {
            return null;
        }
        return coursesKB;
    }

    public static TimeSemester getSemesters(Context context) {
        return JSON.parseObject((String) SharedPreferencesUtil.get(context, SharedPreferencesUtil.SPConstant.SEMESTER, ""), TimeSemester.class);

    }

    public static void setSemesters(Context context, TimeSemester timeSemester) {
        SharedPreferencesUtil.put(context, SharedPreferencesUtil.SPConstant.SEMESTER, JSON.toJSONString(timeSemester));
    }

    public static void removeCourse(Context context,String chooseSemester) {
        SharedPreferencesUtil.remove(context, userInfo.getStudentId() + chooseSemester);
    }
}
